package com.ebc.eservice.util;

import com.ebc.eservice.pojo.EbcUser;
import com.ebc.eservice.pojo.UserDT;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;

/**
 * @author: Joey
 * @Description:获取当前用户id
 * @date:2022/9/20 9:36
 */
public class CurrentUser {

    @Transactional(rollbackFor = Exception.class)
    public int getCurrentUser(){
        EbcUser loginUser = (EbcUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(loginUser);
        if(loginUser==null){
            return 0;
        }
//        System.out.println("当前用户名id"+user.getId());
//        Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
//        System.out.println("->已授予的权限"+authorities);
        return loginUser.getId();
    }

    public EbcUser getCurrentEbcUser(){
        EbcUser loginUser = (EbcUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(loginUser==null){
            return new EbcUser().setId(101);
        }
        return loginUser;
    }

}
